/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espian.showcaseview.sample.animations;

import com.espian.showcaseview.sample.ResourceTable;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.Target;
import com.github.amlcurran.showcaseview.targets.ViewTarget;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

/**
 * 引导层的展示部位可移动
 */
public class AnimationSampleAbility extends Ability implements Component.ClickedListener {
    private ShowcaseView showcaseView;
    private int counter = 0;
    private Text textView1;
    private Text textView2;
    private Text textView3;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_animation, null, false);
        setUIContent(componentContainer);
        textView1 = (Text) findComponentById(ResourceTable.Id_textview1);
        textView2 = (Text) findComponentById(ResourceTable.Id_textview2);
        textView3 = (Text) findComponentById(ResourceTable.Id_textview3);

        showcaseView = new ShowcaseView.Builder(this, (ComponentContainer) componentContainer.getComponentParent())
                .setTarget(new ViewTarget(textView1))
                .setOnClickListener(this)
                .build();
        showcaseView.setButtonText(getString(ResourceTable.String_next));
    }

    private void setAlpha(float alpha, Component... views) {
        for (Component view : views) {
            view.setAlpha(alpha);
        }
    }

    @Override
    public void onClick(Component component) {
        switch (counter) {
            case 0:
                showcaseView.setShowcase(new ViewTarget(textView2), true);
                break;
            case 1:
                showcaseView.setShowcase(new ViewTarget(textView3), true);
                break;
            case 2:
                showcaseView.setTarget(Target.NONE);
                showcaseView.setContentTitle("Check it out");
                showcaseView.setContentText("You don't always need a target to showcase");
                showcaseView.setButtonText(getString(ResourceTable.String_close));
                setAlpha(0.4f, textView1, textView2, textView3);
                break;
            case 3:
                showcaseView.hide();
                setAlpha(1.0f, textView1, textView2, textView3);
                break;
        }
        counter++;
    }

    @Override
    protected void onStop() {
        super.onStop();
        showcaseView.destory();
    }
}
